package chapter5;
import java.util.Scanner;
/****************************************************************
 * Author：icynlau
 * Description: 最长的共同前缀
 * Date:2021/4/4
 * ***************************************************************
 */
public class exc5_51 {
	
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter the first string:");
		String s1=input.nextLine();
		int len1=s1.length();
		System.out.print("Enter the second string:");
		String s2=input.nextLine();
		int len2=s2.length();
		
		int minLen=(len1>len2)?len2:len1;
		
		if(s1.charAt(0)!=s2.charAt(0)) {
			System.out.print(s1+" and "+s2+" have no common prefix.");
			System.exit(1);
		}
		
		int prelen=0;
		System.out.print("The common prefix is ");
		for(int i=0;i<minLen;i++) {
			
			if(s1.charAt(i)==s2.charAt(i)) {
				System.out.print(s1.charAt(i));
			}else {
				break;
			}
			
			
		}
		
		
	}

}
